package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;

public class ZoneDessin extends JPanel {
	private static final long serialVersionUID = 1L;
	
	private ArrayList<Tortue> tortues = new ArrayList<Tortue>();
	private boolean showTurtles=true;
	private Image drawingImage;
	private Dimension dim = new Dimension(400,300);
	
	public void addTortue(Tortue o){
		tortues.add(o);
	}
	
	public void reset(){
		//Dimension dim=getSize();
		//dim =
		
		this.setPreferredSize(new Dimension(400, 300));	// à enlever ?
		
		drawingImage = this.createImage(dim.width,dim.height);
		//drawingImage = this.createImage(300,300);	// Solution temporaire
		//System.out.println("width: "+dim.width+" height:"+dim.height);
		Graphics g = drawingImage.getGraphics();
		Color c = g.getColor();
		g.setColor(Color.white);
		g.fillRect(0,0,dim.width, dim.height);
		g.setColor(c);
		//System.out.println("Reset feuille");
		for(Iterator<Tortue> it = tortues.iterator();it.hasNext();){
			Tortue t = (Tortue) it.next();
			t.reset();
		}
		drawIt();
	}
	

	public void paintComponent(Graphics g){
		if (drawingImage==null)
		  reset();
		g.drawImage(drawingImage,0,0,null);
		if (showTurtles)
		  showTurtles(g);
	}

	public void drawIt(){
		Graphics g = getGraphics();
		g.drawImage(drawingImage,0,0,null);
		if (showTurtles)
		  showTurtles(g);
	}

	public Graphics getImageGraphics(){
		if (drawingImage == null)
			reset();
		return drawingImage.getGraphics();
	}

	void showTurtles(Graphics g){
		for(Iterator<Tortue> it = tortues.iterator();it.hasNext();){
			Tortue t = (Tortue) it.next();
			t.drawTurtle(g);
		}
	}
	
	public Dimension getDim(){
		return dim;
	}
	
}